package com.dreamer.domain.mall.transfer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SumVo {

    private String name;

    private Integer amount;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public static List<SumVo> sum(List<TransferVo> transferVos){
        Map<String,SumVo> sumVoMap = new HashMap<>();
        transferVos.forEach(transferVo -> {
            transferVo.getItemVOs().forEach(item->{
                String name = item.getName();
                Integer amount = item.getQuantity();
                if(transferVo.getAdd()==0){
                    amount = - amount;
                }
                SumVo sumVo ;
                if(sumVoMap.containsKey(name)){
                    sumVo = sumVoMap.get(name);
                    Integer v = sumVo.getAmount() + amount;
                    sumVo.setAmount(v);
                    sumVo.setName(name);
                }else {
                    sumVo = new SumVo();
                    sumVo.setAmount(amount);
                    sumVo.setName(name);
                }
                sumVoMap.put(name,sumVo);
            });
        });
        return new ArrayList<>(sumVoMap.values());
    }

}
